/* 
 * File:   Character.cpp
 * Author: paula
 * 
 * Created on 8 de septiembre de 2014, 23:26
 */

#include "Character.h"

Character::Character() {
}

Character::Character(int x, int y, SDL_Renderer** gRenderer, int player) : Walker(x, y, gRenderer, player) {

    this->myLogFile->getInstance(MY_LOG_MODE);
    this->state = WAITING;
    this->player = player;
    this->loadMediaGrey();

    this->lastState = WAITING;
    Dot* dotito = new Dot(x, y);
    dotito->dotWorldToPixels();
    this->lastPosX = dotito->getPos()->x;
    this->lastPosY = dotito->getPos()->y;
    this->lastSide = true;

    delete dotito;

}

Character::Character(const Character& orig) {
}

void Character::beDead() {

    SDL_Rect* principalSettings = new SDL_Rect();
    SBSImage* image;
    principalSettings->x = this->lastPosX;
    principalSettings->y = this->lastPosY;
    SDL_RendererFlip flip;
    if (this->lastSide) {
        flip = SDL_FLIP_NONE;
    } else {
        flip = SDL_FLIP_HORIZONTAL;
    }
    
//   cout << "antes del swityldgjog     "<<endl;
//           cout<< this->lastState<<endl; 
    
    switch (this->lastState) {
        case JUMPING:
            image = (this->deadJumping);
            break;
        case KNEELING:
            image = (this->deadKneeling);
            break;
        case FALLING:
            image = (this->deadFalling);
            break;
        case WALKING:
            image = (this->deadWalking);
            break;
        case WAITING:
            image = (this->deadWaiting);
            break;
        default:
            image = (this->deadWaiting);
            break;            
    }

    principalSettings->w = (image->getWidth()) * PRINCIPAL_SCALE;
    principalSettings->h = (image->getHeight()) * PRINCIPAL_SCALE;
    principalSettings->x = principalSettings->x - (principalSettings->w / 2);
    principalSettings->y = principalSettings->y - (principalSettings->h / 2);
    this->imageToDraw = image;

    //    SDL_RenderCopyEx(gRenderer, image->getTexture(), NULL, principalSettings, 0, NULL, flip);
    delete principalSettings;

}

void Character::actualizeState(int state) {
    switch (state) {
        case JUMPING:
            this->dead = false;
            this->setJumping();
            break;
        case KNEELING:
            this->dead = false;
            this->setKneeling();
            break;
        case FALLING:
            this->dead = false;
            this->setFalling();
            break;
        case WALKING:
            this->dead = false;
            this->setWalking();
            break;
        case DEAD:
            if (!this->dead) {
                this->lastPosX = this->dot->getPos()->x;
                this->lastPosY = this->dot->getPos()->y;
                this->lastSide = this->getDirectionRight();
                this->dead = true;
            }
            break;
        default:
            this->dead = false;
            this->setWaiting();
            break;
    }
    if (!this->dead) {
        this->lastState = state;
    }
    this->state = state;
}

void Character::loadMediaGrey() {

    deadJumping = new SBSImage();
    deadKneeling = new SBSImage();
    deadWaiting = new SBSImage();
    deadWalking = new SBSImage();
    deadFalling = new SBSImage();
    deadJumping->loadMedia(this->path + "/Grey" + "/uR3.png", &gRenderer);
    deadKneeling->loadMedia(this->path + "/Grey" + "/dR2.png", &gRenderer);
    deadWaiting->loadMedia(this->path + "/Grey" + "/r.png", &gRenderer);
    deadWalking->loadMedia(this->path + "/Grey" + "/r1.png", &gRenderer);
    deadFalling->loadMedia(this->path + "/Grey" + "/uR1.png", &gRenderer);

}

void Character::actualize() {

    this->actualizePosition(this->data.x, this->data.y);
    this->actualizeState(this->data.stateCharacter);
    struct Event receivedEvent = this->data.event;
    this->setDirectionRight(this->data.direction);

    switch (this->getState()) {
        case JUMPING:
            if (receivedEvent.released != NULL) {
                switch (receivedEvent.released) {
                    case 'L':
                        this->stopWalking();
                        break;
                    case 'R':
                        this->stopWalking();
                        break;
                }
            }
            this->keepJumping();
            break;
        case KNEELING:
            if (receivedEvent.released != NULL) {
                switch (receivedEvent.released) {
                    case 'D':
                        this->stopKneeling();
                        break;
                }
            }
            this->keepKneeling();
            break;
        case FALLING:
            if (receivedEvent.pressed != NULL) {
                switch (receivedEvent.pressed) {
                    case 'L':
                        //                                        this->fallLeft();
                        break;
                    case 'R':
                        //                                        this->fallRight();
                        break;
                }
            }
            if (receivedEvent.released != NULL) {
                switch (receivedEvent.released) {
                    case 'L':
                        this->stopWalking();
                        break;
                    case 'R':
                        this->stopWalking();
                        break;
                }
            }
            this->keepFallingDown();
            break;
        case WALKING:
            if (receivedEvent.pressed != NULL) {
                switch (receivedEvent.pressed) {
                    case 'U':
                        this->jump();
                        break;
                }
            }
            if (receivedEvent.released != NULL) {
                switch (receivedEvent.released) {
                    case 'L':
                        if (!this->isWalkingRight()) {
                            this->stopWalking();
                        }
                        break;
                    case 'R':
                        if (!this->isWalkingLeft()) {
                            this->stopWalking();
                        }
                        break;
                }
            }
            this->keepWalking();
            break;
        case DEAD:
            this->beDead();
            break;
        case WAITING:
            this->wait();
            break;
        default:
            this->wait();
            break;
    }
}

Character::~Character() {
//    cout<<"Destructor de Character"<<endl;

    this->freeMediaGrey();
    delete this->dot;
    this->freeMedia();
}

void Character::freeMediaGrey() {
    delete deadJumping;
    delete deadKneeling;
    delete deadWaiting;
    delete deadWalking;
    delete deadFalling;
}
